package com.gopay.common.log.aop;

import com.gopay.common.log.collect.LogBag;
import com.gopay.common.log.collect.LogCollector;
import com.gopay.common.log.constants.LogOperStatusCd;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;

import java.util.List;

public class LogAopAdviceUtil {
    private static Logger logger = Logger.getLogger("biz-log");
    private static Logger errorlogger = Logger.getLogger(LogAopAdviceUtil.class);

    public static void beforeAdvice(LogBag logBag) {
    	try{
    		LogCollector.produce(logBag);
    	}catch(Exception e){
    		errorlogger.error("beforeAdvice方法抛异常",e);
    	}
    }

    public static void afterReturningAdvice(String bagId) {
    	try{
	        int elementIndex = LogCollector.getElementIndex(bagId);
	        boolean isParentHasTransactional = LogCollector.isParentHasTransactional(elementIndex);
	
	        if (!isParentHasTransactional) {
	            List<LogBag> rsList = LogCollector.popSelfAndChildrenByElementIndex(elementIndex);
	
	            LogBag logBag = null;
	            for (int i = 0; i < rsList.size(); i++) {
	                logBag = rsList.get(i);

	                /*******logx2.0 zhoutong 20170707
					if(logBag.getOtBaseVO() instanceof LogOTFindLoginPWDVO){
						LogOTFindLoginPWDVO lflVo = (LogOTFindLoginPWDVO)logBag.getOtBaseVO();
						logBag.setOperDesc(lflVo.toMaskString());
					} else {
						logBag.setOperDesc(logBag.getOtBaseVO().toString());
					}
					 **/


	                //modify by sh.jiao 20170510 去掉vo方式保存操作详情
	                //logBag.setOperDesc(logBag.getOtBaseVO().toString());

	                ObjectMapper mapper = new ObjectMapper();
	                    String jsonVo = mapper.writeValueAsString(logBag);
						//modified by zyt 2017-5-23，对日志做敏感字段掩码操作
	                    //logger.info(jsonVo);
	                    logger.info(logBag.toMaskJsonString());
	                    //logger.info(logBag.toString());
	            }
	        }
    	}catch(Exception e){
    		errorlogger.error("afterReturningAdvice方法抛异常",e);
    	}

    }

    public static void afterThrowingAdvice(String bagId, Exception e) {
    	try{
	        int elementIndex = LogCollector.getElementIndex(bagId);
	        List<LogBag> rsList = LogCollector.popSelfAndChildrenByElementIndex(elementIndex);
	
	        LogBag logBag = null;
	        for (int i = 0; i < rsList.size(); i++) {
	            logBag = rsList.get(i);
				//modify by sh.jiao 20170510 去掉vo方式保存操作详情
                //logBag.setOperDesc(logBag.getOtBaseVO().toString());
                
	
	            // 成功改成回滚
	            if (logBag.getStatus().equals(LogOperStatusCd.SUCCESS.value)) {
	                logBag.setStatus(LogOperStatusCd.ROLLBACK.value);
	            }
	
	            ObjectMapper mapper = new ObjectMapper();
	                String jsonVo = mapper.writeValueAsString(logBag);
	                logger.info(jsonVo);
	            
	        }
    	}catch(Exception e1){
    		errorlogger.error("afterThrowingAdvice方法抛异常",e1);
    	}
    }

}
